package com.jeesite.modules.home.web;


import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.exam.quota.dao.DcSupExamTargetAuditDao;
import com.jeesite.modules.exam.quota.service.DcSupExamTargetAuditService;
import com.jeesite.modules.exam.score.vo.DcSupExamQuotaOrgVo;
import com.jeesite.modules.home.dao.DcMenuStaticsDao;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "${adminPath}/menu/act")
public class DcMenuStaticsController extends BaseController {
    @Autowired
    private SysOfficeUnitService sysOfficeUnitService;
    @Autowired
    private DcMenuStaticsDao dcMenuStaticsDao;


    @RequestMapping(value = "statics")
    public Map menuStatics(String list){
        JSONArray array = JSONArray.parseArray(list);
        Integer currentYear = Integer.parseInt(DateUtils.getYear());
        Map map = new HashMap();
        for (int i = 0; i < array.size(); i++) {
            String nums = (String)array.get(i);
            if (nums.equals("1")){
                // 任务确认
                taskConfirmCount(currentYear,map);
            }else if (nums.equals("2")) {
                // 任务细化分解确认
                taskDetailConfirmCount(currentYear, map);
            }else if (nums.equals("3")) {
                // 任务接收
                taskReceiveCount(currentYear, map);
            }else if (nums.equals("4")) {
                // 任务待办
                taskAgencyCount(currentYear, map);
            }else if (nums.equals("5")) {
                // 待办批示
                taskExplainCount(currentYear, map);
            }else if (nums.equals("6")) {
                // 任务部署审核
                taskDeployAuditCount(currentYear, map);
            }else if (nums.equals("7")) {
                // 任务反馈审核
                taskFeedBackAuditCount(currentYear, map);
            }else if (nums.equals("8")) {
                // 任务细化分解审核
                taskDetailAuditCount(currentYear, map);
            }else if (nums.equals("9")) {
                // 任务整理审核
                taskArrangAuditCount(currentYear, map);
            }else if (nums.equals("10")){
                // 关键指标报送
                reportOfKeyIndicators(currentYear,map);
            }else if (nums.equals("11")){
                // 单项指标报送
                reportOfSingleIndicators(currentYear,map);
            }else if (nums.equals("12")){
                // 细则报送
                detailedRulesForTheSubmitted(currentYear,map);
            }else if (nums.equals("13")){
                // 关键指标评分
                keyIndexScore(currentYear -1,map);
            }else if (nums.equals("14")){
                // 单项指标评分
                singleIndexScore(currentYear -1,map);
            }else if (nums.equals("15")){
                // 指标审核
                indicatorsOfAudit(currentYear,map);
            }
        }
        return map;
    }

    @RequestMapping(value = "statics2")
    public Map menuStatics2(String list){
        JSONArray array = JSONArray.parseArray(list);
        Integer currentYear = Integer.parseInt(DateUtils.getYear());
        String userCode = UserUtils.getUser().getUserCode();
        Map map = new HashMap();
        for (int i = 0; i < array.size(); i++) {
            String nums = (String)array.get(i);
            if (nums.equals("1")){
                // 任务确认
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务确认");
                if(num1!=0){
                    map.put("任务确认",num1) ;
                }
            }else if (nums.equals("2")) {
                // 任务细化分解确认
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务阶段目标报送");
                if(num1!=0){
                    map.put("任务阶段目标报送",num1) ;
                }
            }else if (nums.equals("3")) {
                // 任务接收
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务接收");
                if(num1!=0){
                    map.put("任务接收",num1) ;
                }
            }else if (nums.equals("4")) {
                // 任务待办
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务待办");
                if(num1!=0){
                    map.put("任务待办",num1) ;
                }
            }else if (nums.equals("5")) {
                // 待办批示
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"待办批示");
                if(num1!=0){
                    map.put("待办批示",num1) ;
                }
            }else if (nums.equals("6")) {
                // 任务部署审核
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务部署审核");
                if(num1!=0){
                    map.put("任务部署审核",num1) ;
                }
            }else if (nums.equals("7")) {
                // 任务反馈审核
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务反馈审核");
                if(num1!=0){
                    map.put("任务反馈审核",num1) ;
                }
            }else if (nums.equals("8")) {
                // 任务细化分解审核
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务细化分解审核");
                if(num1!=0){
                    map.put("任务细化分解审核",num1) ;
                }
            }else if (nums.equals("9")) {
                // 任务整理审核
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"任务整理审核");
                if(num1!=0){
                    map.put("任务整理审核",num1) ;
                }
            }else if (nums.equals("10")){
                // 关键指标报送
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"关键指标报送");
                if(num1!=0){
                    map.put("关键指标报送",num1) ;
                }
            }else if (nums.equals("11")){
                // 单项指标报送
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"单项指标报送");
                if(num1!=0){
                    map.put("单项指标报送",num1) ;
                }
            }else if (nums.equals("12")){
                // 细则报送
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"细则报送");
                if(num1!=0){
                    map.put("细则报送",num1) ;
                }
            }else if (nums.equals("13")){
                // 关键指标评分
                Long aLong = keyIndexScore2();
                map.put("关键指标评分",aLong);
            }else if (nums.equals("14")){
                // 单项指标评分
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"单项指标评分");
                if(num1!=0){
                    map.put("单项指标评分",num1) ;
                }
            }else if (nums.equals("15")){
                // 指标审核
                Integer num1 = dcMenuStaticsDao.queryBacklogCount(userCode,"指标审核");
                if(num1!=0){
                    map.put("指标审核",num1) ;
                }
            }
        }
        return map;
    }

    @RequestMapping(value = "statics3")
    public Map menuStatics3(String list){
        JSONArray array = JSONArray.parseArray(list);
        String userCode = UserUtils.getUser().getUserCode();
        Map map = new HashMap();
        List<Map<String,Object>> listRes = dcMenuStaticsDao.getbacklogGroupCount(userCode);
        for (int i = 0; i < listRes.size(); i++) {
            map.put(listRes.get(i).get("backlogType"),listRes.get(i).get("num")) ;
        }
        Long aLong = keyIndexScore2();
        map.put("关键指标评分",aLong);
        return map;
    }

    /**
     * 需要任务确认
     */
    public void taskConfirmCount(Integer currentYear,Map map){
        Integer count =  dcMenuStaticsDao.queryTaskConfirmCount(currentYear, OfficeUtil.getCurrentOffice().getOfficeCode());
        map.put("任务确认",count);
    }

    /**
     * 需要任务细化分解确认
     */
    public void taskDetailConfirmCount( Integer currentYear,Map map){
        Integer count =  dcMenuStaticsDao.queryTaskDetailConfirmCount(currentYear,sysOfficeUnitService.
                getLatestUnitByOffice().getOfficeCode());
        map.put("任务细化分解确认",count);
    }


    /**
     * 需要任务接收
     */
    public void taskReceiveCount( Integer currentYear,Map map){
        Integer count =  dcMenuStaticsDao.queryTaskReceiveCount(currentYear,sysOfficeUnitService.
                getLatestUnitByOffice().getOfficeCode());
        map.put("任务接收",count);
    }


    /**
     * 任务待办
     */
    public void taskAgencyCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        String currentOfficeCode = OfficeUtil.getCurrentOffice().getOfficeCode();
        Integer count =  dcMenuStaticsDao.taskAgencyCount(currentYear,userCode,currentOfficeCode);
        map.put("任务待办",count);
    }

    /**
     * 待办批示
     */
    public void taskExplainCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        Integer count =  dcMenuStaticsDao.taskExplainCount(currentYear,userCode);
        map.put("待办批示",count);
    }

    /**
     * 任务部署审核
     */
    public void taskDeployAuditCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        Integer count =  dcMenuStaticsDao.taskDeployAuditCount(currentYear,userCode);
        map.put("任务部署审核",count);
    }

    /**
     * 任务反馈审核
     */
    public void taskFeedBackAuditCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        Integer count =  dcMenuStaticsDao.taskFeedBackAuditCount(currentYear,userCode);
        map.put("任务反馈审核",count);
    }

    /**
     * 任务细化分解审核
     */
    public void taskDetailAuditCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        Integer count =  dcMenuStaticsDao.taskDetailAuditCount(currentYear,userCode);
        map.put("阶段目标报送审核",count);
    }

    /**
     * 任务整理审核
     */
    public void taskArrangAuditCount( Integer currentYear,Map map){
        String userCode = UserUtils.getUser().getUserCode();
        Integer count =  dcMenuStaticsDao.taskArrangAuditCount(currentYear,userCode);
        map.put("任务整理审核",count);
    }

    /**
     * 关键指标报送
     * 统计待接收状态
     */
    public void  reportOfKeyIndicators(Integer currentYear,Map map){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
      Integer count=  dcMenuStaticsDao.reportOfKeyIndicators(currentYear,"1","1",userCode);
        map.put("关键指标报送",count);
    }
    /**
     * 单项指标报送
     * 统计待接收状态
     */
    public void  reportOfSingleIndicators(Integer currentYear,Map map){
        String userCode =sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        Integer count=  dcMenuStaticsDao.reportOfKeyIndicators(currentYear,"2","1",userCode);
        map.put("单项指标报送",count);
    }
    /**
     * 细则报送 Detailed rules for the submitted
     * 统计待接收状态
     */
    public void  detailedRulesForTheSubmitted(Integer currentYear,Map map){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        Integer count=  dcMenuStaticsDao.reportOfKeyIndicators(currentYear,"5","1",userCode);
        map.put("细则报送",count);
    }

    /**
     * 关键指标评分 Detailed rules for the submitted
     *  未评分状态显示角标
     */
    public void  keyIndexScore(Integer currentYear,Map map){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        // 查询当前机构所涉及的考核任务
        List<Map<String,String>> quotaOrgVos = dcMenuStaticsDao.keyIndexScore(currentYear, "1",userCode);
        // 查询评分状态
        List<Map<String,String>> scoreStatus = dcMenuStaticsDao.scoreInfos(currentYear ,"2",userCode);

        Long result = filterScore(quotaOrgVos,scoreStatus);
        map.put("关键指标评分",result);
    }
    public Long keyIndexScore2(){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        // 查询当前机构所涉及的考核任务
        List<Map<String,String>> quotaOrgVos = dcMenuStaticsDao.keyIndexScore2( "1",userCode);
        // 查询评分状态
        List<Map<String,String>> scoreStatus = dcMenuStaticsDao.scoreInfos2("2",userCode);

        return filterScore(quotaOrgVos,scoreStatus);
    }

    /**
     * 单项指标评分 Detailed rules for the submitted
     * 未评分状态显示角标
     */
    public void  singleIndexScore(Integer currentYear,Map map){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        // 查询当前机构所涉及的考核任务
        List<Map<String,String>> quotaOrgVos = dcMenuStaticsDao.keyIndexScore(currentYear, "2",userCode);
        // 查询评分状态
        List<Map<String,String>> scoreStatus = dcMenuStaticsDao.scoreInfos(currentYear ,"3",userCode);

        Long result = filterScore(quotaOrgVos,scoreStatus);
        map.put("单项指标评分",result);
    }

    /**
     * 指标审核 Detailed rules for the submitted
     * 统计接收状态 但未处理的 角标
     */
    public void  indicatorsOfAudit(Integer currentYear,Map map){
        String userCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        // 关键指标查询 已接收未报送状态的数据信息  quota_group = '1'  and  form_type='1' 的数据信息
        //20220907 审核角标展示  只展示未审核数据信息
        // 关键
         Integer mapList = convertData(currentYear, "1");
        // 单项
        Integer single = convertData(currentYear,"2");
        // 细则审核
        Integer xiZeCount = convertXiZe(currentYear, userCode);

        map.put("指标审核",mapList+single+xiZeCount);
    }

    private Integer convertXiZe(Integer currentYear, String userCode) {
        List<Map<String, String>> mapList=   dcMenuStaticsDao.exameOrgListData(currentYear);
        if (CollectionUtils.isEmpty(mapList)){
            return 0;
        }
        List<Map<String, String>> mapList1 = mapList.stream().filter(s -> "2".equals(s.get("filestatus"))).collect(Collectors.toList());
        return mapList1.size();
    }


    private Map<String,Map<String,String>> convertListToMap(List<Map<String,String>> result){

        Map<String,Map<String,String>> returnInfo =new HashMap<>(result.size());
        for (int i = 0; i < result.size(); i++) {
            Map<String, String> map = result.get(i);
             returnInfo.put(map.get("examId"),map);
        }
        return  returnInfo;
    }


    private Long filterScore(List<Map<String,String>> quotaOrgVos,List<Map<String,String>> scoreStatus){
        Long result = 0L;
        if (!CollectionUtils.isEmpty(quotaOrgVos)){

            if (CollectionUtils.isEmpty(scoreStatus)){

                for (int i = 0; i < quotaOrgVos.size(); i++) {
                    // 考核评分机构数量
                    Object amount = quotaOrgVos.get(i).get("nums");
                    if (amount instanceof BigDecimal) {
                        BigDecimal nums = (BigDecimal) amount;
                        result = result+nums.longValue();
                    }else if (amount instanceof Long){
                        Long   nums = (Long) amount;
                        result =result + nums;
                    }

                }
            }else{

                Map<String,Map<String,String>> listToMap = convertListToMap(scoreStatus);

                for (int i = 0; i < quotaOrgVos.size(); i++) {
                    Map<String, String> stringMap = quotaOrgVos.get(i);
                    // 考核id
                    String examId = stringMap.get("examId");
                    // 考核评分机构数量
                    Object amount= stringMap.get("nums");
                    Long nums=0L;
                    if (amount instanceof Long){
                        nums = (Long) amount;
                    }else if (amount instanceof BigDecimal){
                        nums = ((BigDecimal)amount).longValue();
                    }

                    if (listToMap.containsKey(examId)){
                        Object num2= listToMap.get(examId).get("nums");
                        Long compareNums = 0L;
                        if (num2 instanceof Long){
                            compareNums = (Long) num2;
                        }
                        if (compareNums == nums){
                            continue;
                        }else{
                            nums = nums-compareNums;
                        }
                    }
                    result =result + nums;
                }
            }
        }
        return result;
    }


    private Integer convertData(Integer currentYear,String type){
        List<Map<String, String>> list = dcMenuStaticsDao.exameSendOrgListData(currentYear,  type);
        if(!list.isEmpty()){
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).get("submitstatus").contains(",")){
                    String[] s=list.get(i).get("submitstatus").split(",");
                    list.get(i).put("submitstatus",s[0]);
                }
            }
            List<Map<String, String>> maps = list.stream().filter(m -> "5".equals(m.get("submitstatus")) || "2".equals(m.get("submitstatus"))).collect(Collectors.toList());
            return maps.size();
        }
        return 0;
    }


}
